#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "rngs.h"
#include "interface.h"


int randInt(int low, int high)
{
	int temp = low + rand() % (high -low +1);
	return temp;

}

void printHandtoFile(int player, struct gameState* state, FILE* gameResults){

	//prints hand to file because function in interface file does not print to file
	int handCount = state->handCount[player];
	int handIndex;
	int card;
	char name[100];
	fprintf(gameResults, "Player %d's hand:\n", player);
	if(handCount > 0) fprintf(gameResults, "#  Card\n");
		for(handIndex = 0; handIndex < handCount; handIndex++) {
	   		card = state->hand[player][handIndex];
	    	cardNumToName(card, name);
	    	fprintf(gameResults, "%-2d %-13s\n", handIndex, name);
	     }
}



void testActionPhase(int player, struct gameState* state, FILE* gameResults){
	int i;
	int card;
	char name[100];
	int isAction;
	int count;
	int deckcount;

	count = state->numActions;

	while (count > 0){
	   for (i = 0; i < numHandCards(state); i++) {
	    card = handCard(i, state);
	    //Find an action card and play it
	    	if (card > adventurer && card <= treasure_map){
	    	    	cardNumToName(card, name);
	    	    	fprintf(gameResults, "\tI have an action card to play %s\n", name);
	    	    	fprintf(gameResults,"\tPlaying card %s\n", name);
	    	        playCard(card,1,1,1,state);
	    	        deckcount =  fullDeckCount(player, card, state);

	    	        count--;
	    	        isAction = 1;
	    	        break;
	    	    }
	    	}

	   	   //no action card to play
	    	if (isAction != 1){
	    		fprintf(gameResults,"\tNo action card to play\n");
	    	    count = 0;
	    	}
	  }
}

//structure borrowed from Alex Carpenter
void testBuyPhase(int money, int *kingdomCards, struct gameState* state, FILE* gameResults) {

	int randCard;
	int playCard_error;
	int count;
	count = 0;

	while (state->numBuys > 0) {
		//make sure when I play a card I don't get an error
		playCard_error = -1;

		if (money >= 8) {
			playCard_error = buyCard(province, state);
			if (playCard_error == 0) { fprintf(gameResults, "\tBought province card\n"); }
		}
		else if (money == 6) {
			randCard = floor(Random() * 2);
			if (randCard == 0) {
				playCard_error = buyCard(gold, state);
				if (playCard_error == 0) { fprintf(gameResults,"\tBought gold card!\n"); }
			}
			else if (randCard == 1) {
				playCard_error = buyCard(adventurer, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought adventurer card!\n");}
			}
		}
		else if (money == 5) {
			randCard = floor(Random() * 7);
			if (randCard == 0) {
				playCard_error = buyCard(duchy, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought duchy card!\n");}
			}
			if (randCard == 1) {
				playCard_error = buyCard(silver, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought silver card!\n");}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(council_room, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought council_room card!\n");}

			}
			else if (randCard == 3) {
				playCard_error = buyCard(mine, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought mine card!\n");}

			}
			else if (randCard == 4) {
				playCard_error = buyCard(minion, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought minion card!\n");}
			} else if (randCard == 5) {
				playCard_error = buyCard(tribute, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought tribute card!\n");}
			}
			else if (randCard == 6) {
				playCard_error = buyCard(outpost, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought outpost card!\n");	}
			}
		}
		else if (money == 4) {
			randCard = floor(Random() * 10);
			if (randCard == 0) {
				playCard_error = buyCard(silver, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought silver card!\n");}
			}
			if (randCard == 1) {
				playCard_error = buyCard(feast, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought feast card!\n");}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(gardens, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought gardens card\n");}
			}
			else if (randCard == 3) {
				playCard_error = buyCard(remodel, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought remodel card!\n");}
			}
			else if (randCard == 4) {
				playCard_error = buyCard(smithy, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought smithy card!\n");}
			}
			else if (randCard == 5) {
				playCard_error = buyCard(baron, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought baron card!\n");}
			}
			else if (randCard == 6) {
				playCard_error = buyCard(cutpurse, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought cutpurse card!\n");}
			}
			else if (randCard == 7) {
				playCard_error = buyCard(salvager, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought salvager card!\n");}
			}
			else if (randCard == 8) {
				playCard_error = buyCard(sea_hag, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought sea_hag card!\n");}
			}
			else if (randCard == 9) {
				playCard_error = buyCard(treasure_map, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought treasure_map card!\n");}
			}
		}
		else if (money == 3) {
			randCard = floor(Random() * 5);
			if (randCard == 0) {
				playCard_error = buyCard(silver, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought silver card!\n");}
			}
			else if (randCard == 1) {
				playCard_error = buyCard(village, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought village card!\n");}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(great_hall, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought great_hall card!\n");}
			}
			else if (randCard == 3) {
				playCard_error = buyCard(steward, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought steward card!\n");}
			}
			else if (randCard == 4) {
				playCard_error = buyCard(ambassador, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought ambassador card!\n");}
			}
		}
		else if (money == 2) {
			randCard = floor(Random() * 3);
			if (randCard == 0) {
				playCard_error = buyCard(estate, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought estate card!\n");}
			}
			else if (randCard == 1) {
				playCard_error = buyCard(copper, state);
				if (playCard_error == 1) {fprintf(gameResults, "\tBought copper card!\n");}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(embargo, state);
				if (playCard_error == 1) {fprintf(gameResults, "\tBought embargo card!\n");}
			}
		}
		else if (money >= 0) {
			randCard = floor(Random() * 2);
			if (randCard == 0) {
				playCard_error = buyCard(copper, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought copper card!\n");}
			} else if (randCard == 1) {
				playCard_error = buyCard(curse, state);
				if (playCard_error == 0) {fprintf(gameResults, "\tBought curse card!\n");}
			}
		}

		count++;

		if (count > 50 || playCard_error == 0) {
			if (count > 50) {
				fprintf(gameResults, "Did not buy anything\n");
				state->numBuys--;
			}
		count = 0;
		}

	}

}



void playGame(int seed, FILE* gameResults){

	int n;
	int i;
	int numPlayers;
	int player;
	int totalPlayers[MAX_PLAYERS];
	int success;
	int k[10];
	char kingName[100];
	int money = 0;

	//set up how many random games to play
	for (n = 0; n < 100; n++) {

	newGame();
	fprintf(gameResults, "Initializing game...%d\n", n);
	struct gameState G;
	numPlayers = floor(Random() * MAX_PLAYERS); //make number of players random
	  if (numPlayers < 2){ 	numPlayers = 2;  }
	  else if (numPlayers > 4){	numPlayers= 4;  }

	  //generate random kingdom cards
	  selectKingdomCards(rand(),k);

	  //print number of players
	  fprintf(gameResults, "\nNumber of players are: %d", numPlayers);

	  //print kingdom cards in hand
	  for (i = 0; i < 10; i++){
	  	cardNumToName(k[i], kingName);
	  	fprintf(gameResults, "\nKingdom cards in game is: %s\n", kingName);
	  }

	//Initialize Game
	success = initializeGame(numPlayers, k, seed, &G);

	if (success != 0) {
		fprintf(gameResults, "Initializing Game Error");
		exit(200);
	}

    while (!isGameOver(&G)){

    	/*Give each player a turn*/
    	for(player = 0;player < numPlayers;player++){

    		//print the hand
    		printHandtoFile(player, &G, gameResults);

    		money = 0;
    		i=0;

    		//count coins in hand
    		int money = countHandCoins(player, &G);

    		//Action Phase
    	    fprintf(gameResults,"Give me some Action\n");
    	    G.phase = 0;
    	    testActionPhase(player, &G, gameResults);

    	    //Buy Phase
    	    fprintf(gameResults, "Ready to Buy some shit!\n");
    	    G.phase = 1;
    	    testBuyPhase(money, k, &G, gameResults);

    	    //CleanUp
    	    G.phase = 2;
    	    fprintf(gameResults, "Player's turn is over %d\n\n", player);
    	    endTurn(&G);

    		} //end for each player

    	} //end while loop

		//get winners
		getWinners(totalPlayers, &G);
		fprintf(gameResults, "Game Over\n");
		for (i = 0; i < G.numPlayers; i++){
			fprintf(gameResults, "\tPlayer %d score: %d\n", i, scoreFor(i, &G));
		}

	} // for loop for how many games

}


int main (int argc, char** argv)
{


	int seed;
	char filename[] = "gameresults.out";
	FILE *gameResults;
	gameResults = fopen(filename, "w");

	seed = rand();
	SelectStream(1);
	PutSeed(seed);

	playGame(seed, gameResults);

	return 0;
}

